package com.company.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import model.Libro;
import model.ListaUtile;
import model.Opcione;
import model.Persona;
import model.Tutor;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.omg.CORBA.PRIVATE_MEMBER;

import com.company.common.util.Utilitario;

@Action(value="registrar-utiles",
results={
	@Result(name="exito", location="/utiles/registrar-utiles.jsp"),
	@Result(name="error", location="/inicio.jsp")
} )
public class RegistrarUtilesAction implements SessionAware{

	private Map<String, Object> session;
	
	public Map<String, Object> getSession() {
	     return session;
	}
	public void setSession(Map<String, Object> session) {
	     this.session = session;
	}
	
	private List<Libro> libros;
	private Tutor tutor;
	private ListaUtile listaUtiles;

	public List<Libro> getLibros() {
		return libros;
	}
	public void setLibros(List<Libro> libros) {
		this.libros = libros;
	}
	public Tutor getTutor() {
		return tutor;
	}
	public void setTutor(Tutor tutor) {
		this.tutor = tutor;
	}
	public ListaUtile getListaUtiles() {
		return listaUtiles;
	}
	public void setListaUtiles(ListaUtile listaUtiles) {
		this.listaUtiles = listaUtiles;
	}
	public String execute(){
		String result="exito";
		
		Persona tmp = (Persona)session.get("persona");
		//Como el registrar utiles solo lo realizara el tutor, buscaremos en tutor
		EntityManager em = Utilitario.getInstance().getEntityManager();
		EntityTransaction tx=em.getTransaction();
		
		Query q = em.createQuery("SELECT t FROM Tutor t WHERE t.idPersona = :idPersona");
		q.setParameter("idPersona", tmp.getIdPersona());
		Tutor tutorbd;
		
		try{
			tutorbd = (Tutor) q.getSingleResult();
		}catch(NoResultException e){
			result="error";
			tutorbd = null;
		}
		session.put("tutor", tutorbd);
		
		Query q2 = em.createQuery("SELECT l FROM Libro l WHERE l.grado = :grado ");
		q2.setParameter("grado", tutorbd.getGrado());
		
		try{
			libros = (List<Libro>) q2.getResultList();
		}catch(NoResultException e){
			result="error";
			libros = null;
		}
		
		try{
			tx.begin();
			Query max = em.createQuery("SELECT max(l.idLista_Utiles) FROM ListaUtile l");
			Integer tmpLista;
			try{
				tmpLista = Integer.parseInt(max.getSingleResult()+"");
			}catch(NoResultException e){
				tmpLista = null;
			}
			
			if(tmpLista==null){
				listaUtiles.setIdLista_Utiles(1);
			}else{
				listaUtiles.setIdLista_Utiles(tmpLista+1);
			}
			listaUtiles.setEstado("1");
			listaUtiles.setIdPersona(tutorbd.getIdPersona());
			listaUtiles.setIdTutor(tutorbd.getIdTutor());
			SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
			String fechaActual = sf.format(new Date());
			listaUtiles.setFecha(fechaActual);
			
			em.persist(listaUtiles);
			tx.commit();
			session.put("message", "Registro Satisfactorio");
		}catch(Exception e){
			System.err.println("Error"+e.getMessage());
		}
		
		return result;
	}

}

